package com.maidou.qiantai.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.maidou.qiantai.vo.spu.SpuCategoryTreeVo;
import com.maidou.qiantai.vo.spu.SpuRecommendVo;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.maidou.qiantai.mapper.SpuCategoryMapper;
import com.maidou.qiantai.domain.SpuCategory;
import com.maidou.qiantai.service.ISpuCategoryService;

import javax.annotation.Resource;

/**
 * 商品三级分类Service业务层处理
 * 
 * @author maidou
 * @date 2024-03-13
 */
@Service
public class SpuCategoryServiceImpl implements ISpuCategoryService 
{
    @Resource
    private SpuCategoryMapper spuCategoryMapper;

    /**
     * 查询商品三级分类
     * 
     * @param id 商品三级分类主键
     * @return 商品三级分类
     */
    @Override
    public SpuCategory selectSpuCategoryById(Long id)
    {
        return spuCategoryMapper.selectSpuCategoryById(id);
    }

    /**
     * 查询商品三级分类列表
     * 
     * @param spuCategory 商品三级分类
     * @return 商品三级分类
     */
    @Override
    public List<SpuCategory> selectSpuCategoryList(SpuCategory spuCategory)
    {
        return spuCategoryMapper.selectSpuCategoryList(spuCategory);
    }

    /**
     * 新增商品三级分类
     * 
     * @param spuCategory 商品三级分类
     * @return 结果
     */
    @Override
    public int insertSpuCategory(SpuCategory spuCategory)
    {
        spuCategory.setCreateTime(DateUtils.getNowDate());
        return spuCategoryMapper.insertSpuCategory(spuCategory);
    }

    /**
     * 修改商品三级分类
     * 
     * @param spuCategory 商品三级分类
     * @return 结果
     */
    @Override
    public int updateSpuCategory(SpuCategory spuCategory)
    {
        spuCategory.setUpdateTime(DateUtils.getNowDate());
        return spuCategoryMapper.updateSpuCategory(spuCategory);
    }

    /**
     * 批量删除商品三级分类
     * 
     * @param ids 需要删除的商品三级分类主键
     * @return 结果
     */
    @Override
    public int deleteSpuCategoryByIds(Long[] ids)
    {
        return spuCategoryMapper.deleteSpuCategoryByIds(ids);
    }

    /**
     * 删除商品三级分类信息
     * 
     * @param id 商品三级分类主键
     * @return 结果
     */
    @Override
    public int deleteSpuCategoryById(Long id)
    {
        return spuCategoryMapper.deleteSpuCategoryById(id);
    }

    /**
     * 获取三级分类树形结构
     * @return
     */
    @Override
    public List<SpuCategoryTreeVo> selectSpuCategoryListTree() {
        //获取三级分类所有数据
        List<SpuCategory> spuCategories = spuCategoryMapper.selectSpuCategoryList(new SpuCategory());
        //获取三级分类一级分类数据,并且封装为SpuCategoryTreeVo对象
        List<SpuCategoryTreeVo> oneSpuCategoryTreeList = spuCategories.stream().filter((item) -> {
            return item.getCatLevel() == 0;
        }).map(item->{
            SpuCategoryTreeVo spuCategoryTreeVo = new SpuCategoryTreeVo();
            BeanUtils.copyProperties(item,spuCategoryTreeVo);
            return spuCategoryTreeVo;
        }).collect(Collectors.toList());
        //获取三级分类二级分类数据，并封装为SpuCategoryTreeVo对象
        List<SpuCategoryTreeVo> twoSpuCategoryTreeList = spuCategories.stream().filter((item) -> {
            return item.getCatLevel() == 1;
        }).map(item -> {
            SpuCategoryTreeVo spuCategoryTreeVo = new SpuCategoryTreeVo();
            BeanUtils.copyProperties(item, spuCategoryTreeVo);
            return spuCategoryTreeVo;
        }).collect(Collectors.toList());
        //获取三级分类三级分类数据，并封装为SpuCategoryTreeVo对象
        List<SpuCategoryTreeVo> threeSpuCategoryTreeList = spuCategories.stream().filter((item) -> {
            return item.getCatLevel() == 2;
        }).map(item -> {
            SpuCategoryTreeVo spuCategoryTreeVo = new SpuCategoryTreeVo();
            BeanUtils.copyProperties(item, spuCategoryTreeVo);
            return spuCategoryTreeVo;
        }).collect(Collectors.toList());
        //封装二级分类
        List<SpuCategoryTreeVo> twoCollect = twoSpuCategoryTreeList.stream().map(item -> {
            Long id = item.getId();
            ArrayList<SpuCategoryTreeVo> children = new ArrayList<>();
            for (SpuCategoryTreeVo spuCategoryTreeVo :
                    threeSpuCategoryTreeList) {
                if (spuCategoryTreeVo.getParentId() == id) {
                    children.add(spuCategoryTreeVo);
                }
            }
            item.setChildren(children);
            return item;
        }).collect(Collectors.toList());
        //封装一级分类
        List<SpuCategoryTreeVo> oneCollect = oneSpuCategoryTreeList.stream().map(item -> {
            Long id = item.getId();
            ArrayList<SpuCategoryTreeVo> children = new ArrayList<>();
            for (SpuCategoryTreeVo spuCategoryTreeVo :
                    twoCollect) {
                if (spuCategoryTreeVo.getParentId() == id) {
                    children.add(spuCategoryTreeVo);
                }
            }
            item.setChildren(children);
            return item;
        }).collect(Collectors.toList());
        return oneCollect;
    }
}
